package org.proteored.miapeapi.xml.pride;

import java.util.Date;
import java.util.List;

import org.proteored.miapeapi.cv.ControlVocabularyManager;
import org.proteored.miapeapi.exceptions.IllegalMiapeArgumentException;
import org.proteored.miapeapi.exceptions.MiapeDatabaseException;
import org.proteored.miapeapi.exceptions.MiapeSecurityException;
import org.proteored.miapeapi.interfaces.MiapeDate;
import org.proteored.miapeapi.interfaces.MiapeDocument;
import org.proteored.miapeapi.interfaces.Project;
import org.proteored.miapeapi.interfaces.User;
import org.proteored.miapeapi.interfaces.ms.MSContact;
import org.proteored.miapeapi.interfaces.persistence.PersistenceManager;
import org.proteored.miapeapi.xml.pride.autogenerated.ExperimentType;
import org.proteored.miapeapi.xml.pride.autogenerated.PersonType;
import org.proteored.miapeapi.xml.pride.util.InstrumentManager;

public abstract class AbstractDocumentFromPride implements MiapeDocument {
	protected final ExperimentType experiment;
	protected PersistenceManager dbManager;
	protected final ControlVocabularyManager cvManager;
	protected final InstrumentManager instrumentManager;
	protected final User user;
	protected final String projectName;
	protected final MSContact contact;
	public String url;
	protected final String prideXMLFileName;

	public AbstractDocumentFromPride(ExperimentType experiment, ControlVocabularyManager cvManager,
			User user, String prideXMLFileName, String projectName)
			throws IllegalMiapeArgumentException {
		this.experiment = experiment;
		this.cvManager = cvManager;
		this.prideXMLFileName = prideXMLFileName;
		if (experiment.getMzData() != null && experiment.getMzData().getDescription() != null
				&& experiment.getMzData().getDescription().getInstrument() != null) {
			this.instrumentManager = new InstrumentManager(experiment.getMzData().getDescription()
					.getInstrument(), cvManager);
		} else {
			this.instrumentManager = new InstrumentManager(cvManager);
		}

		this.user = user;
		this.contact = createContact();

		this.projectName = projectName;
	}

	protected AbstractDocumentFromPride(ExperimentType experiment, PersistenceManager dbManager,
			ControlVocabularyManager cvManager, String user, String password,
			String prideXMLFileName, String projectName) throws MiapeDatabaseException,
			MiapeSecurityException, IllegalMiapeArgumentException {
		this(experiment, cvManager, dbManager.getUser(user, password), prideXMLFileName,
				projectName);
		this.dbManager = dbManager;
	}

	@Override
	public MSContact getContact() {
		return contact;
	}

	private MSContact createContact() throws IllegalMiapeArgumentException {
		List<PersonType> contactXML = experiment.getMzData().getDescription().getAdmin()
				.getContact();
		PersonType firstContact = contactXML.get(0);
		MSContact contact = new ContactImpl(firstContact, user);
		return contact;
	}

	@Override
	public MiapeDate getDate() {
		return new MiapeDate(new Date(System.currentTimeMillis()));
	}

	@Override
	public int getId() {
		// Not generated yet.
		return -1;
	}

	@Override
	public Date getModificationDate() {
		return new Date(System.currentTimeMillis());
	}

	@Override
	public User getOwner() {
		return user;
	}

	@Override
	public String getAttachedFileLocation() {
		return url;
	}

	@Override
	public Project getProject() {
		return new ProjectImpl(projectName, user, this.dbManager);
	}

	@Override
	public Boolean getTemplate() {
		return false;
	}

	@Override
	public String getVersion() {
		return "1.0 Created by Miape API";
	}

	public void setAttachedFileLocation(String url) {
		this.url = url;
	}
}
